//动态生成表格,给show函数传递相应的参数就行:
var mydata = [];
var mydata2 = [];
//功能选择
var select;

//自调用事件
function login() {
	console.log(1)
	$.ajax({
		type: "post", //post put get 等等
		//url:"http://img.zhangjiankeji.cn/ashx/DStu_XL.ashx",
		url: "http://img.zhangjiankeji.cn/ashx/DStuCus.ashx",
		data: { //要传入ashx文件的数据	
			"orderid": "",
			"Key": "",
			"begin": $(".firstdate").val(),
			"end": $(".lastdate").val()
		},
		success: function(data, textStatus) {

			//判断返回错误情况
			if (data == '请选择查询日期！' || data == '[]') {
				alert('请选择查询日期或日期不正确');
				return;
			}
			console.log(JSON.parse(data));
			mydata = JSON.parse(data);
			if (mydata) {
				table_1();
			}

		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			alert('返回错误');
			alert(errorThrown);
			//连接至ashx文件失败时，执行函数
			//XMLHttpRequest在这个例子里没有用到
			//textStatus是表示状态的字符串，这里textStatus的值是"error"
			//errorThrown包含连接失败的信息，可以输出查看
		}
	});

};


//触发按钮时运行此函数
function show(i) {

	console.log(i);
	switch (i) {
		case 0:
			$(".breadcrumb>li:nth-child(2)").text('豪迈佳型材采购');
			break;
		case 1:
			$(".breadcrumb>li:nth-child(2)").text('功能2');
			break;
		case 2:
			$(".breadcrumb>li:nth-child(2)").text('功能3');
			break;
	}

}
//此方法负责加载表格数据-表格第一个!!!
function table_1() {
	var str = '';
	str =
		`<h4 style="padding:10px;">用户订单</h4>
				<ul class="nav nav_tit">
					<li>序</li>
					<li><input type="checkbox" onclick="allselect(event)"></li>
					<li>订单号</li>
					<li>金额</li>
					<li>备注</li>
					<li>操作</li>
				</ul>		
				`

	for (var i = 0; i < mydata.length; i++) {
		str +=
			`
						<ul class="nav nav_body">	
							<li>
								<span> </span>
								<span class="nav_body_id">${i + 1}</span>
							</li>
							<li>
								<span> </span>
								<span>							
										<input type="checkbox" data-id="${i}">
								</span>
							</li>
						
							<li onclick="StuOrder(${i})">
								<span> </span>
								<span>${mydata[i].OrderID}</span>
							</li>
							<li>
								<span></span>
								<span>${mydata[i].OutPrice}￥</span>
							</li>
							<li>
								<span>备注:</span>
								<span>${mydata[i].Remark}</span>
							</li>
							
							<hr style="width:100%;" class="my_hr"/>
							<li>
								<span> </span>
								<span onclick="href1(${i})" class="text-info">添加</span> &nbsp;
								<span onclick="StuOrder(${i})" class="text-info">修改</span> &nbsp;
								<span onclick="delet(${i})" class="text-info">拷贝</span> &nbsp;
								<span onclick="delet(${i})" class="text-danger">删除</span>
								
							</li>		
										
						</ul>
					`
	}
	d1.innerHTML = str;
}

//全选功能
function allselect(event) {
	var checkedOfAll = $(event.target).prop("checked");
	$('.nav_body').find(":checkbox").prop("checked", checkedOfAll);

}
//全选功能
function allselect2(event) {
	var checkedOfAll = $(event.target).prop("checked");
	$('.nav_body2').find(":checkbox").prop("checked", checkedOfAll);

}


//订单详情事件
function StuOrder(i) {
	console.log(mydata[i].StuOrderOutCusID);
	$.ajax({
		type: "post", //post put get 等等
		url: "http://img.zhangjiankeji.cn/StuWeb/ashx/DStuCusItem.ashx",
		data: { //要传入ashx文件的数据	
			"OutID": mydata[i].StuOrderOutCusID,
		},
		success: function(data, textStatus) {
			//判断返回错误情况
			console.log(JSON.parse(data));
			mydata2 = JSON.parse(data);
			if (mydata2) {
				console.log(document.body.clientWidth)
				if (document.body.clientWidth > 760) {
					table_2();
				} else {
					window.open(`./2.html?id=${mydata[i].StuOrderOutCusID}`);
				}

			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			alert('返回错误');
			alert(errorThrown);
		}
	});


}

//此方法负责加载表格数据-表格第二个!!!
function table_2() {
	var str = '';
	str =
		`<h4 style="padding: 10px;" class="table_2_h4">详情页
		<span onclick="screen()" class="glyphicon glyphicon-sort" style="color: rgb(100, 100, 100);"></span></h4>
		<ul class="nav nav_tit2">
			<li><input type="checkbox" onclick="allselect2(event)"></li>
			<li>序号</li>
			<li>名称</li>
			<li>产品型号</li>
			<li>图形</li>
			<li>产品规格</li>
			<li>单位</li>
			<li>颜色</li>
			<li>米重</li>
			<li>理论重</li>
			<li>长度</li>
			<li>壁厚</li>
			<li>数量</li>
			<li>单价</li>
			<li>金额</li>
			<li>备注</li>
		</ul>		
		`

	for (var i = 0; i < mydata2.length; i++) {
		str +=
			`
				<ul class="nav nav_body2">
					<li>
						<span>选择:</span>
						<span>							
								<input type="checkbox" data-id="${i}">
						</span>
					</li>
					<li>
						<span>序号:</span>
						<span>${i + 1}</span>
					</li>
					<li>
						<span>名称:</span>
						<span>${mydata2[i].DName}</span>
					</li>
					<li>
						<span>产品型号:</span>
						<span>${mydata2[i].DMode}</span>
					</li>
					<li>
						<span>图形:</span>
						<span><img src="DStuffXHImg/${mydata2[i].DMode}.jpg" height="50" width="50"/></span>
					</li>
					<li>
						<span>产品规格:</span>
						<span>${mydata2[i].DGge}</span>
					</li>
						<li>
						<span>单位:</span>
						<span>${mydata2[i].Unit}</span>
					</li>
						<li>
						<span>颜色:</span>
						<span>${mydata2[i].ColorName}</span>
					</li>
						<li>
						<span>米重:</span>
						<span>${mydata2[i].DWght}</span>
					</li>
					<li>
						<span>理论重:</span>
						<span>${mydata2[i].Weight}</span>
					</li>
					<li>
						<span>长度:</span>
						<span>${mydata2[i].DLong}</span>
					</li>
					<li>
						<span>壁厚:</span>
						<span>${mydata2[i].BiHou}</span>
					</li>
					<li>
						<span>数量:</span>
						<span><input onchange="changnum(event,${i})" style="width:50px;" class="dnums" type="number" value="${mydata2[i].DNums}"></span>
						
					</li>
					<li>
						<span>单价:</span>
						<span>${mydata2[i].DPrice}</span>
					</li>
					<li>
						<span>金额:</span>
						<span>${mydata2[i].SumPrice}</span>
					</li>
					<li>
						<span>注释:</span>
						<span><input  style="width:100px;" class="remark" type="text" value="${mydata2[i].Remark}"></span>
					</li>
				</ul>
			`
	}
	str =
		`<div>${str}</div>	
		<div class="buttom">
			<button type="button" class="btn btn-info" onclick='update()'>选择修改</button><button type="button" class="btn btn-danger" onclick='del()'>选择删除</button>
		</div>`
	d2.innerHTML = str;
}

//跳转到第3个页面-(产品页面)--带参数跳转
function href1(i) {
	if(document.body.clientWidth > 760){
		window.open(`./3.html?id=${mydata[i].StuOrderOutCusID}`);
	}else{
		window.open(`./handset/addto.html?id=${mydata[i].StuOrderOutCusID}`);
	}	
}
//不带参数跳转-到第三个页面
function href_3(){
	if(document.body.clientWidth > 760){
		window.open(`./3.html`);
	}else{
		window.open(`./handset/addto.html`);
	}	
}
//删除选中的d1中的数据
function dele_all() {
	if (window.confirm("您确定要删除么？")) {
		//遍历选中的数组
		$(".nav_body input:checked").each(function() {
			console.log($(this).attr("data-id"))
			var i = $(this).attr("data-id")
			//对选中的元素进行删除
			var a = mydata.splice(typeof Number(i), 1);

		})
		//重新渲染页面
		table_1();

	} else {
		return;
	}
}

//修改选中的d2表格
function update() {

	//遍历选中的数组
	$(".nav_body2 input:checked").each(function() {
		console.log($(this).attr("data-id"));
		var i = $(this).attr("data-id");
		//对选中的元素的数量和备注进行修改
		mydata2[Number(i)].DNums = $(this).parents(".nav_body2").find('.dnums').val();
		mydata2[Number(i)].Remark = $(this).parents(".nav_body2").find('.remark').val();
	})
	//重新渲染页面
	table_2();
}

//删除选中的d2表格
function del() {
	if (window.confirm("您确定要删除么？")) {

		//遍历选中的数组
		$(".nav_body2 input:checked").each(function() {
			console.log($(this).attr("data-id"));
			var i = $(this).attr("data-id")
			//对选中的元素进行删除
			var a = mydata2.splice(typeof Number(i), 1);
		})
		//重新渲染页面
		table_2();

	} else {
		return;
	}
}

//数量改变引起重量,金额的改变
function changnum(e,i){
	console.log(e)
	mydata2[i].DNums=$(e.target).val();
		console.log(i)
	// 重量=长度/1000* 米重*数量-(只保留两位小数)
	mydata2[i].Weight=Number(mydata2[i].DLong/1000*mydata2[i].DNums*mydata2[i].DWght).toFixed(2);
	if(mydata2[i].DWght){
		// 金额=单价* 重量
	mydata2[i].SumPrice=mydata2[i].DPrice*mydata2[i].Weight;
	}else{
		// 没有米重的情况下。金额=数量*单价
	mydata2[i].SumPrice=mydata2[i].DPrice*mydata2[i].DNums;
	}
	//重新渲染页面
	table_2();
}
//排序方法
function screen(){
	console.log(1)
	$.ajax({
			type: "post", //post put get 等等
			//url:"http://img.zhangjiankeji.cn/ashx/DStu_XL.ashx",
			url: "http://img.zhangjiankeji.cn/ashx/DStu_XL_Item.ashx",
			data: { //要传入ashx文件的数据	
				'DBIndex':'0',
				'XLMeID':colorid,
				'ColorID':select_color.value
			},
			success(data){
				console.log(data);
			}
				
				
			})

}


